package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.DateUtil;
import ysq.xyg.web.dao.WebSignUpDao;
import ysq.xyg.web.model.po.WebSignUp;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.vo.WebSignUpVo;
import ysq.xyg.web.service.WebSignUpService;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * 业务模块-我要报名service实现
 *
 * @author shengQiang yu
 * @date 2019-4-25 16:05:25
 */
@Service("webSignUpService")
public class WebSignUpServiceImpl extends BaseServiceImpl<WebSignUp> implements WebSignUpService {

    @Resource
    private WebSignUpDao webSignUpDao;

    private WebSignUp webSignUp;

    @Autowired
    public void setBaseDao(WebSignUpDao webSignUpDao) {
        this.baseDao = webSignUpDao;
    }
    /**
     * 分页带数量查询
     *
     * @param webSignUpVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-25 16:05:25
     */
    public ReturnEntity pageLeft(WebSignUpVo webSignUpVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" sort desc,update_time desc");
        List<WebSignUp> list = webSignUpDao.leftList(webSignUpVo);
        PageInfo<WebSignUp> page=new PageInfo<WebSignUp>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 分页查询
     *
     * @param webSignUpVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-25 16:05:25
     */
    public ReturnEntity page(WebSignUpVo webSignUpVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<WebSignUp> list = acceptList(webSignUpVo);
        PageInfo<WebSignUp> page=new PageInfo<WebSignUp>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webSignUpVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-25 16:05:25
     */
    public ReturnEntity addOrUpdate(WebSignUpVo webSignUpVo, SysUser currentUser) throws Exception {
        boolean isUpate=true;
        if(StrUtil.isEmpty(webSignUpVo.getId())){
            webSignUp=new WebSignUp(currentUser);
            isUpate=false;
        }else{
            webSignUp=selectByPrimaryKey(webSignUpVo.getId());
            if(ObjectUtil.isNull(webSignUp)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            webSignUp.setUpdateTime(new Date());
            webSignUp.setUpdateUser(currentUser.getId());
        }
        webSignUp.setUserType(webSignUpVo.getUserType());
        webSignUp.setStartAge(webSignUpVo.getStartAge());
        webSignUp.setEndAge(webSignUpVo.getEndAge());
        webSignUp.setTitle(webSignUpVo.getTitle());
        webSignUp.setType(webSignUpVo.getType());
        webSignUp.setPicture(webSignUpVo.getPicture());
        if(StrUtil.isNotEmpty(webSignUpVo.getStartDate())){
            webSignUp.setStartDate(DateUtil.getTextDate(webSignUpVo.getStartDate(),"yyyy-MM-dd"));
        }
        if(StrUtil.isNotEmpty(webSignUpVo.getEndDate())){
            webSignUp.setEndDate(DateUtil.getTextDate(webSignUpVo.getEndDate(),"yyyy-MM-dd"));
        }
        webSignUp.setContent(webSignUpVo.getContent());
        webSignUp.setStatus(webSignUpVo.getStatus());
        webSignUp.setSort(webSignUpVo.getSort());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(webSignUp);
        }else{
            num=insert(webSignUp);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-25 16:05:25
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebSignUp webSignUp=selectByPrimaryKey(id);
        if(webSignUp!=null){
            webSignUp.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webSignUp.setUpdateTime(new Date());
            webSignUp.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(webSignUp);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合左关联
     *
     * @param webSignUpVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-4-25 16:05:25
     */
    public List<WebSignUp> acceptListLeft(WebSignUpVo webSignUpVo){
        return webSignUpDao.leftList(webSignUpVo);
    }
    /**
     * 获取集合
     *
     * @param webSignUpVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-4-25 16:05:25
     */
    public List<WebSignUp> acceptList(WebSignUpVo webSignUpVo){
        Example example = new Example(WebSignUp.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webSignUpVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",webSignUpVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(webSignUpVo.getType())){
            criteria.andEqualTo("type",webSignUpVo.getType());
        }
        if(StringUtil.isNotEmpty(webSignUpVo.getUserType())){
            criteria.andEqualTo("userType",webSignUpVo.getUserType());
        }
        example.setOrderByClause(" update_time desc");
        return webSignUpDao.selectByExample(example);
    }
}
